package com.neusoft.ruidaozaixian.controller.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.neusoft.ruidaozaixian.R;
import com.neusoft.ruidaozaixian.model.bean.PostCourse;
import com.neusoft.ruidaozaixian.util.MyApplication;

import java.util.List;


public class PostCourseAdapter extends BaseAdapter {
    private final String TAG = getClass().getSimpleName();
    private static ImageLoader mImageLoader;
    private LayoutInflater mInflater;
    private List<PostCourse.IndexPositionListBean> beanList;
    private Context context;



    public PostCourseAdapter(Context context, List<PostCourse.IndexPositionListBean> beanList) {
        super();

        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        this.beanList = beanList;
        mImageLoader = MyApplication.getInstance().getImageLoader();


    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_postcourse, null);
            holder.img= (ImageView) convertView.findViewById(R.id.m);
            holder.postId=(TextView)convertView.findViewById(R.id.postId);
            holder.postName = (TextView) convertView.findViewById(R.id.postNameTv);
            holder.personNums = (TextView) convertView.findViewById(R.id.personNumsTv);
            holder.courseNums = (TextView) convertView.findViewById(R.id.courseNumsTv);
            holder.courseHours = (TextView) convertView.findViewById(R.id.courseHoursTv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PostCourse.IndexPositionListBean  bean= beanList.get(position);
        Glide
                .with(context)
                .load(bean.getPostUrl())
                .centerCrop()
                  .placeholder(R.mipmap.ic_launcher)//默认图
                .crossFade()
                .into(holder.img);

//      holder.imsg.setImageURI(bean.getPostUrl());
//   String postId= String.valueOf(bean.getPostId());
        holder.postId.setText(bean.getPostId());//用于传递PostId不做显示
        holder.postName.setText(bean.getPostName());
        holder.personNums.setText("当正在学习有："+bean.getPersonNums()+"人");
        holder.courseNums.setText("总课程："+bean.getCourseNums()+"门");
        holder.courseHours.setText("总课时："+bean.getCourseHours()+"小时");



        return convertView;

    }

    private static class ViewHolder {
        ImageView img;
        TextView postId;
        TextView postName;
        TextView personNums;
        TextView courseNums;
        TextView courseHours;


    }

    /**
     * 刷新列表
     *
     * @param newList
     */
    public void notifyDataSetChanged(List<PostCourse.IndexPositionListBean> newList) {
        this.beanList = newList;
        notifyDataSetChanged();
    }

}
